/**   
* @Title: CaiWuXiangGuanServiceImpl.java 
* @Package com.zjdiepu.www.biz.zpp 
* 
* @author zhangpp
* @date 2017年5月26日 下午8:46:29 
* @version V1.0   
*/
package com.zjdiepu.www.biz.zpp;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.cw.CwRebateMapper;
import com.zjdiepu.www.dao.dh.DhDepositMoneyMapper;
import com.zjdiepu.www.dao.dh.DhOverdueListHistoryMapper;
import com.zjdiepu.www.dao.dh.DhOverdueListMapper;
import com.zjdiepu.www.dao.dh.DhSettleMapper;
import com.zjdiepu.www.dao.fk.FkAuditListMapper;
import com.zjdiepu.www.dao.yw.YwBusinessMapper;
import com.zjdiepu.www.dao.zpp.CaiWuXiangGuanDao;
import com.zjdiepu.www.dao.zpp.PublicDao;
import com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService;
import com.zjdiepu.www.ibiz.zpp.DaiZhongYwService;
import com.zjdiepu.www.model.cw.dto.CwRebate;
import com.zjdiepu.www.model.dh.dto.DhDepositMoney;
import com.zjdiepu.www.model.dh.dto.DhSettle;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.zpp.cwxg.vo.DengJiYwTiChengVo;
import com.zjdiepu.www.model.zpp.cwxg.vo.RefundInformationVo;
import com.zjdiepu.www.model.zpp.cwxg.vo.TuiYaJinListVo;
import com.zjdiepu.www.model.zpp.dzyw.vo.BusinessVo;
import com.zjdiepu.www.model.zpp.pub.ext.ParamsExt;
import com.zjdiepu.www.util.CommonUtil;

/**      
 * 项目名称：diepu-parent-biz   
 * 类名称：CaiWuXiangGuanServiceImpl   
 * 类描述：   
 * 创建人：zhangpp
 * 创建时间：2017年5月26日 下午8:46:29   
 * 修改人：zhangpp
 * 修改时间：2017年5月26日 下午8:46:29   
 * 修改备注：   
 * @version V1.0     
 */
@Service
public class CaiWuXiangGuanServiceImpl implements CaiWuXiangGuanService
{

	@Autowired
	private CaiWuXiangGuanDao caiWuXiangGuanDao;
	
	@Autowired
	private DhSettleMapper dhSettleMapper;
	
	@Autowired
	private YwBusinessMapper ywBusinessMapper;
	
	@Autowired
	private CwRebateMapper cwRebateMapper;
	
	@Autowired
	private DhDepositMoneyMapper dhDepositMoneyMapper;
	
	@Autowired
	private FkAuditListMapper fkAuditListMapper;
	
	@Autowired
	private DaiZhongYwService daiHouYwService;
	
	@Autowired
	private DhOverdueListMapper dhOverdueListMapper;
	
	@Autowired
	private DhOverdueListHistoryMapper dhOverdueListHistoryMapper;
	
	@Autowired
	private PublicDao publicDao;

	/* 
	* @author zhangpp
	* <p>Title: caiWuXiangGuanKeHuJieQing</p> 
	* <p>Description: </p> 
	* @param dhSettle
	* @param ywBusiness
	* @param currentUser 
	* @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#caiWuXiangGuanKeHuJieQing(com.zjdiepu.www.model.dh.dto.DhSettle, com.zjdiepu.www.model.yw.dto.YwBusiness, com.zjdiepu.www.base.core.AbstractSysUser) 
	* @version V1.0
	*/
	@Override
	public void diepuCaiWuXiangGuanKeHuJieQing(DhSettle dhSettle,
			YwBusiness ywBusiness, AbstractSysUser currentUser) {
		
		
		//String userId = currentUser.getId();
		String creatorName = currentUser.getName();
		String busiId = dhSettle.getBusiId();
		
		//查询业务信息表
		ywBusiness = ywBusinessMapper.selectByPrimaryKey(busiId);
		if(null == ywBusiness || ("").equals(ywBusiness))
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"ywBusiness is null");
		}
		
		//修改业务信息状态 
		ywBusiness.setBusiStatus(9999);
		int updateCount = ywBusinessMapper.updateByPrimaryKeySelective(ywBusiness);
		if(updateCount < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"ywBusinessMapper error");
		} 
		
		//查询
		DhSettle dSettle = new DhSettle();
		dSettle.setBusiId(busiId);
		dSettle.setDpStatus(1);
		dSettle = dhSettleMapper.selectRefined(dSettle);
		if(null == dSettle)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhSettleMapper is null");
		}
		
		//修改结清表数据
		dhSettle.setAuditor(creatorName);
		dhSettle.setId(dSettle.getId());
		int count = dhSettleMapper.updateByPrimaryKeySelective(dhSettle);
		if(count < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhSettleMapper error");
		}
		
		//查询客户逾期表数据
		String sqlXmls = "select (max(t.overdue_count_total_real))count from dh_overduelist_history t where t.busi_id = '"+busiId+"' and t.dp_status = 1";
		List<Map<String, String>> mapsList = publicDao.queryWhereSql(sqlXmls);
		int numOverdue = 0;
		if(null != mapsList && mapsList.size() > 0)
		{
			Map<String, String>  mpas = mapsList.get(0);
			
			JSONObject jsonObject = CommonUtil.getJSONObject(mpas);
			
			if(null != jsonObject.get("count") && !("").equals(jsonObject.get("count")) && !jsonObject.get("count").equals("null"))
			{
				numOverdue = jsonObject.getInt("count");
			}
		}
		
		//更新逾期表数据
		DhSettle settle = new DhSettle();
		settle.setId(dhSettle.getId());
		settle.setNumOverdue(numOverdue);
		count = dhSettleMapper.updateByPrimaryKeySelective(settle);
		if(count < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhSettleMapper error");
		}
	}

	/* 
	* @author zhangpp
	* <p>Title: getRefundInformationEntity</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @return 
	* @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#getRefundInformationEntity(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt) 
	* @version V1.0
	*/
	@Override
	public RefundInformationVo getRefundInformationEntity(ParamsExt paramsExt) {
		
		return caiWuXiangGuanDao.getRefundInformationEntity(paramsExt);
	}

	/* 
	* @author zhangpp
	* <p>Title: getKeHuJieQingList</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @return 
	* @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#getKeHuJieQingList(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt) 
	* @version V1.0
	*/
	@Override
	public List<BusinessVo> getKeHuJieQingList(ParamsExt paramsExt) {
		
		return caiWuXiangGuanDao.getKeHuJieQingList(paramsExt);
	}

	/* 
	* @author zhangpp
	* <p>Title: getKeHuJieQingCount</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @return 
	* @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#getKeHuJieQingCount(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt) 
	* @version V1.0
	*/
	@Override
	public int getKeHuJieQingCount(ParamsExt paramsExt) {
		
		return caiWuXiangGuanDao.getKeHuJieQingCount(paramsExt);
	}

	/* 
	* @author zhangpp
	* <p>Title: getTuiYaJinList</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @return 
	* @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#getTuiYaJinList(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt) 
	* @version V1.0
	*/
	@Override
	public List<TuiYaJinListVo> getTuiYaJinList(ParamsExt paramsExt) {
		
		return caiWuXiangGuanDao.getTuiYaJinList(paramsExt);
	}

	/* 
	* @author zhangpp
	* <p>Title: getTuiYaJinCount</p> 
	* <p>Description: </p> 
	* @param paramsExt
	* @return 
	* @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#getTuiYaJinCount(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt) 
	* @version V1.0
	*/
	@Override
	public int getTuiYaJinCount(ParamsExt paramsExt) {
		
		return caiWuXiangGuanDao.getTuiYaJinCount(paramsExt);
	}

	/*  
	*  @author  zhangpp
	*  <p>Title:  getDengJiYwTiChengVo</p>  
	*  <p>Description:  </p>  
	*  @param paramsExt
	*  @return  
	*  @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#getDengJiYwTiChengVo(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt)  
	*  @version  V1.0
	*/
	@Override
	public List<DengJiYwTiChengVo> getDengJiYwTiChengVo(ParamsExt paramsExt) {
		
		return caiWuXiangGuanDao.getDengJiYwTiChengVo(paramsExt);
	}

	/*  
	*  @author  zhangpp
	*  <p>Title:  getDengJiYwTiChengCount</p>  
	*  <p>Description:  </p>  
	*  @param paramsExt
	*  @return  
	*  @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#getDengJiYwTiChengCount(com.zjdiepu.www.model.zpp.pub.ext.ParamsExt)  
	*  @version  V1.0
	*/
	@Override
	public int getDengJiYwTiChengCount(ParamsExt paramsExt) {
		
		return caiWuXiangGuanDao.getDengJiYwTiChengCount(paramsExt);
	}

	/*  
	*  @author  zhangpp
	*  <p>Title:  ywTiChengSave</p>  
	*  <p>Description:  </p>  
	*  @param currentUser
	*  @param cwRebate  
	*  @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#ywTiChengSave(com.zjdiepu.www.base.core.AbstractSysUser, com.zjdiepu.www.model.cw.dto.CwRebate)  
	*  @version  V1.0
	*/
	@Override
	public void diepuYwTiChengSave(AbstractSysUser currentUser, CwRebate cwRebate) 
	{
		
		
		String userId = currentUser.getId();
		String loginName = currentUser.getName();
		
		//业务编号
		String busiId = cwRebate.getBusiId();
		//返利金额
		double amountRebate = cwRebate.getAmountRebate();
		
		//查询返利表
		CwRebate rebate = new CwRebate();
		rebate.setBusiId(busiId);
		rebate.setDpStatus(1);
		rebate = cwRebateMapper.selectRefined(cwRebate);
		
		if(null == rebate || ("").equals(rebate))
		{
			//添加
			cwRebate.initInsertData(userId, loginName);
			int inserCount = cwRebateMapper.insert(cwRebate);
			if(inserCount == 0)
			{
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),"cwRebate insert fail");
			} 
			return;
		}
		
		
		//修改
		CwRebate bateCwRebate = new CwRebate();
		bateCwRebate.setId(rebate.getId());
		bateCwRebate.setAmountRebate(amountRebate);
		int updateCount = cwRebateMapper.updateByPrimaryKeySelective(bateCwRebate);
		if(updateCount == 0)
		{
			throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
		} 
		else if(updateCount > 1)
		{
			throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
		}
		
	}

	/*  
	*  @author  zhangpp
	*  <p>Title:  tuiYaPriceUp</p>  
	*  <p>Description:  </p>  
	*  @param currentUser
	*  @param dhDepositMoney  
	*  @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#tuiYaPriceUp(com.zjdiepu.www.base.core.AbstractSysUser, com.zjdiepu.www.model.dh.dto.DhDepositMoney)  
	*  @version  V1.0
	*/
	@Override
	public void diepuTuiYaPriceUp(AbstractSysUser currentUser,DhDepositMoney dhDepositMoney) 
	{
		
		dhDepositMoney.setStatusRefundDeposit(100);
		int count = dhDepositMoneyMapper.updateByPrimaryKeySelective(dhDepositMoney);
		if(count < 1)
		{
			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhDepositMoneyMapper updateByPrimaryKeySelective error");
		}
	}

	/*  
	*  @author  zhangpp
	*  <p>Title:  diepuKeHuJiQing</p>  
	*  <p>Description:  </p>  
	*  @param currentUser
	*  @param dhSettle  
	*  @see com.zjdiepu.www.ibiz.zpp.CaiWuXiangGuanService#diepuKeHuJiQing(com.zjdiepu.www.base.core.AbstractSysUser, com.zjdiepu.www.model.dh.dto.DhSettle)  
	*  @version  V1.0
	*/
	@Override
	public void diepuKeHuJiQing(AbstractSysUser currentUser, DhSettle dhSettle) {
		
		
//		String userId = currentUser.getId();
//		String loginName = currentUser.getName();
//		String busiId = dhSettle.getBusiId();
//		
//		dhSettle.initInsertData(userId, loginName);
//		int count = dhSettleMapper.insert(dhSettle);
//		if(count < 1)
//		{
//			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhSettleMapper error");
//		}
		
//		//查询逾期表
//		DhOverdueList dhOverdueList = new DhOverdueList();
//		dhOverdueList.setBusiId(busiId);
//		dhOverdueList.setDpStatus(1);
//		dhOverdueList = dhOverdueListMapper.selectRefined(dhOverdueList);
//		if(null == dhOverdueList)
//		{
//			return;
//		}
//		
//		String sqlXmls = "select max(t.overdue_count_continuous) overdue_count_continuous,max(t.overdue_count_total)overdue_count_total  from dh_overduelist_history t where t.busi_id = '"+userId+"' and t.dp_status = 1";
//		List<Map<String, String>> list = publicDao.queryWhereSql(sqlXmls);
//		if(null == list || 0 == list.size())
//		{
//			return;
//		}
//		
//		Map<String, String> object = list.get(0);
//		JSONObject objs = CommonUtil.getJSONObject(object);
//		int overdueCountContinuous = 0;
//		int overdueCountTotal = 0;
//		
//		if(null !=  objs.get("overdueCountContinuous"))
//		{
//			overdueCountContinuous = objs.getInt("overdueCountContinuous");
//		}
//		
//		if(null !=  objs.get("overdueCountTotal"))
//		{
//			overdueCountTotal = objs.getInt("overdueCountTotal");
//		}
//		
//		dhOverdueList.setOverdueCountContinuous(overdueCountContinuous);
//		dhOverdueList.setOverdueCountTotal(overdueCountTotal); 
//		count = dhOverdueListMapper.updateByPrimaryKeySelective(dhOverdueList);
//		if(count < 1)
//		{
//			throw new BaseException(ResultFlagType.FAIL.getCode().toString(),"dhOverdueListMapper error");
//		}
	}
}
